package com.zzx.handler;

import com.alibaba.fastjson.JSON;
import com.zzx.exception.ExceptionState;
import com.zzx.util.ResponseResult;
import com.zzx.util.ResponseWrite;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
/*认证异常类*/
public class AuthenicationException implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        System.out.println( authException.getMessage());
        ResponseResult<String> stringResponseResult;
       if( authException.getClass().equals(BadCredentialsException.class)){
           stringResponseResult = new ResponseResult<>(ExceptionState.UNPASSWORD);
       }else{
           stringResponseResult = new ResponseResult<>(ExceptionState.AUTHENICATIONEXCEPTION);
       }
        String s = JSON.toJSONString(stringResponseResult);
        ResponseWrite.ResponseString(response,s);
    }
}
